
    <div class="content margin_top">
        <header class="header fixed_top bg_mx_green"></header>
        <form class="report">
            <div class="bg_white margin_b_10">
                <div class="padding_h_15 padding_v_5 border_b lineheight_3">
                    <div class="flex">
                        <div class="width_70">负责人：</div>
                        <div class="left_70"><{$person['personname']}></div>
                    </div>
                    <div class="flex">
                        <div class="width_70">地区：</div>
                        <div class="left_70 flex">
                            <div class="flex_1">
                                <select class="select1" name="province" id="">
                                    <option value="">县</option>
                                </select>
                            </div>
                            <div class="flex_1">
                                <select class="select2" name="city" id="">
                                    <option value="">乡/镇</option>
                                </select>
                            </div>
                            <div class="flex_1">
                                <select class="select3" name="area" id="">
                                    <option value="">村</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="flex">
                        <div class="width_70">投放数量：</div>
                        <div class="left_70 total">0</div>
                    </div>
                </div>
            </div>
            <div class="bg_white padding_10_15 margin_b_10 lineheight_2">
                <div class="open_list mx_green arrow_down">已投放列表</div>
                <div class="font_12 report_list hide"></div>
            </div>
            <div class="bg_white margin_b_10 padding_10_15 lineheight_2 font_12 report_content"></div>
            <div class="padding_15 font_16">
                <button class="btn btn_mx_green all_width lineheight_3">保存</button>
            </div>
        <input name="_csrf" type="hidden" id="_csrf" value="<{$app->request->csrfToken}>">
        </form>
        <!--<div class="contract fixed all bg_dark white padding_15 hide" style="z-index:20">-->
            <!--<div class="absolute font_18 contract_close" style="right:15px;">-->
                <!--<div class="iconfont icon-close"></div>-->
            <!--</div>-->
            <!--<div class="text_center font_16 lineheight_3">《牧心农业农户代养仔猪合同》</div>-->
            <!--<div class="text_indent lineheight_2">牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同牧心农业农户代养仔猪合同</div>-->
        <!--</div>-->
    </div>
    <div class="toast"></div>
    <!-- toast模版 -->
    <script type="text/template" charset="utf-8" id='toast_template'>
        <i class="iconfont icon-toast{{=it.icon}}"></i>
        <div class="toast_text">{{=it.text}}</div>
    </script>
    <script type="text/template" charset="utf-8" id='record_template'>
        <div class="flex text_center border_b lineheight_3">
            <div class="col_2">投放编号</div>
            <div class="col_5">投放地址</div>
            <div class="col_3">投放农户</div>
            <div class="col_2">投放数量</div>
        </div>
        <div style="max-height:300px;overflow:auto;">
            {{for(var i=0;i<it.length;i++){}}
            <div class="flex text_center border_b">
                <div class="col_2 border_r">{{=i+1}}</div>
                <div class="col_5 border_r">{{=it[i].address}}</div>
                <div class="col_3 border_r">{{=it[i].farmername}}</div>
                <div class="col_2">{{=it[i].num}}</div>
            </div>
            {{}}}
        </div>
    </script>
    <script type="text/template" charset="utf-8" id='info_template'>
        <div class="flex">
            <div class="col_4 padding_h_5">投放编号</div>
            <div class="col_4 padding_h_5">投放农户</div>
            <div class="col_4 padding_h_5">投放数量</div>
        </div>
        {{for(var i=0;i<it.data.length;i++){}}
        <div class="flex data_one">
            <div class="col_4 margin_5">{{=i+1}}</div>
            <div class="col_4 border margin_5 padding_h_5">
                <select class="font_12" name="farmerid[]" request title="农户">
                {{for(var j=0;j<it.farmer_list.length;j++){}}
                    <option value="{{=it.farmer_list[j].id}}" {{if(it.farmer_list[j].id==it.data[i].farmerid){}}selected{{}}}>{{=it.farmer_list[j].farmer}}</option>
                {{}}}
                </select>
                <input type="hidden" name="farmername[]" value="{{=it.data[i].farmername}}">
            </div>
            <div class="col_4 border margin_5 padding_h_5">
                <input class="font_12" type="text" name="num[]" value="{{=it.data[i].num||''}}" placeholder="数量" request title="数量" data-pattern="^[1-5]\d*$" data-tips="1到5之间的数字">
            </div>
        </div>
        {{}}}
        <div class="block_inline label_mx_green padding_h_5 margin_5" onclick="addline()">+添加一条</div>
        {{if(it.data.length>0){}}
        <div class="block_inline label_mx_green padding_h_5 margin_5" onclick="delline()">-删除一条</div>
        {{}}}
        <!--<div class="font_12"><span class="danger">*</span> 代养成功表示认可<span class="info" onclick="opencontract()">《牧心农业农户代养仔猪合同》</span></div>-->
    </script>
    <script type="text/template" charset="utf-8" id='area_template'>
        {{if(Object.keys(it.data).length){}}
            {{for(var i in it.data){ }}
                <option value="{{=i}}"{{ if(it.val==i){ }}selected{{ } }}>{{=it.data[i].name}}</option>
            {{ } }}
        {{}else{}}
            <option value="">暂无</option>
        {{}}}
    </script>
    <script>
        // 城市联动数据初始化
            var address = <{$address}>;
            console.log(address);
            var evalarea = doT.template($("#area_template").text());
            var evalToast = doT.template($("#toast_template").text());
            level({ele:".select",data:address,level1:'',level2:'',level3:'',func:evalarea});
        // 页面初始化
            var farmer = <{$farmer}>,villageid = $(".select3").val(),total = 0,total_init = 0;
            var pagedata = {farmer_list:farmer_init(villageid),data:[{farmerid:"",num:"",farmername:farmer[0].farmer}]};
            var evalinfo = doT.template($("#info_template").text());
            loaddata(".report_content",evalinfo,pagedata);
            loadheadbar("投放计划上报",1);
        // 农户列表
            function farmer_init(n){
                var data = [];
                for (var i = 0; i < farmer.length; i++) {
                    if(farmer[i].villageid == n){
                        data.push(farmer[i]);
                    }
                }
                if(!data.length){
                    toast("wrong","该村没有可选农户，请重新选择")
                }
                return data;
            }
            $(".select3").change(function(){
                pagedata.farmer_list = farmer_init($(this).val());
                loaddata(".report_content",evalinfo,pagedata);
            })
        // 投放列表
            //var records = [{farmerid:"",num:2,address:"柞水营盘镇营房村",farmername:farmer[0].farmer}]
            var records = <{$records}>;
            var evalrecord = doT.template($("#record_template").text());
            loaddata(".report_list",evalrecord,records);
            for (var i = 0; i < records.length; i++) {
                total_init += parseInt(records[i].num);
            }
            $(".total").html(total_init);
            $(".open_list").click(function(){
                $(this).toggleClass("arrow_up arrow_down")
                $(".report_list").toggle()
            })
        // 合同浮层
            function opencontract(){
                $(".contract").slideToggle();
            }
            $(".contract_close").click(function(){
                $(".contract").slideToggle();
            });
        // 增加一行
            function addline(){
                if(pagedata.data.length && !checkedit()) return false;
                var obj = {farmerid:"",num:0,farmername:pagedata.farmer_list[0].farmer};
                pagedata.data.push(obj);
                loaddata(".report_content",evalinfo,pagedata);
            }
        // 删除一行
            function delline(){
                var len = pagedata.data.length;
                total -= parseInt(pagedata.data[len-1].num);
                $(".total").html(total);
                pagedata.data.splice(len-1,1);
                loaddata(".report_content",evalinfo,pagedata);
            }
        // 检测编辑内容
            function checkedit(){
                var datas = pagedata.data;
                var len = datas.length;
                var eles = $(".data_one");
                var j = true;
                total = total_init;
                for (var i = 0; i < len; i++) {
                    var ele_id = eles.eq(i).find("select[name='farmerid[]']");
                    var ele_num = eles.eq(i).find("input[name='num[]']");
                    if($(ele_id[0]).attr("request")==""){
                        if(!checkone(ele_id[0])){
                            j = false;
                            return false;
                        }
                    }
                    if($(ele_num[0]).attr("request")==""){
                        if(!checkone(ele_num[0])){
                            j = false;
                            return false;
                        }
                    }
                    datas[i].farmerid = ele_id.val();
                    datas[i].num = ele_num.val();
                    total += parseInt(ele_num.val());
                    $(".total").html(total);
                    for (var j = 0; j < farmer.length; j++) {
                        if(farmer[j].id == ele_id.val()){
                            datas[i].farmername = farmer[j].farmer;
                        }
                    }
                }
                return j;
            }
        // 提交表单
            var submited = false;
            $(".report").submit(function(e){
                e.preventDefault();
                with(this){
                    for (var i = 0; i < this.length; i++) {
                        if($(this[i]).attr("request")==""){
                            if(!checkone(this[i])){
                                return false;
                            }
                        }
                    }
                    if(submited) return false;
                    if(pagedata.data.length && !checkedit()) return false;
                    var data = $(".report").serialize();
                    $.post("/business/person/post-delivery-plan",data,function(res){console.log(res);
                        toast("attention",res.msg)
                        if(res.status == 0){
                            submited = true;
                            location.href='/business/person/farmer'
                        }
                    },"json")
                }
            })
    </script>